# 测试用户登陆页面

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from common.Utils import BlogDriver

# 创建类
class BlogLogin:
    url = ""
    driver = ""

    def __init__(self):
        # 博客登录页面url
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    def TestCase(self,user,password):
        """
        测试用例
        :return:
        """
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(user)
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

    def Popupwaiting(self):
        """
        用于获取弹窗消息
        :return:
        """
        # 等待提示框 WebDriverWait等待5秒直到弹框出现，不然会报错
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert  # 获取弹窗警告
        print ("提示："+alert.text)
        # 确认弹窗
        alert.accept()

    def BlogSucLogin(self):
        """
        用于测试登录页面用户成功登录
        :return:
        """
        print("----测试用户成功登录----")
        user="zhangsan"
        password="123456"
        # 输入正确的账号和密码(输入前先清空输入框)
        self.TestCase(user, password)

        #判断用户是否登录成功
        #添加隐式等待
        self.driver.implicitly_wait(5)
        name = self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3").text
        #对name进行断言
        assert name
        essay = self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)").text
        text=self.driver.title
        print("登录成功\n当前用户:{}-pwd:{}\n文章数量：{}\n当前页面为：{}".format(user, password,essay,text))
        print("----测试结束----\n")

        #添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)

    def BlogFailLogin(self):
        """
        用于测试登录页面用户登录失败
        :return:
        """
        print("----测试用户登录失败----")
        #输入正确的账号和错误的密码
        print("测试用例1：输入正确的账号和错误的密码  账号：zhangsan 	密码：123456789")
        self.TestCase(user="zhangsan", password="123456789")
        self.Popupwaiting()

        print("测试用例1：账号：zhangsan 	密码：123")
        self.TestCase(user="zhangsan", password="123")
        self.Popupwaiting()

        print("测试用例2：输入输入错误的账号和正确的密码  账号：666 	密码：123456")
        self.TestCase(user="666",password="123456")
        #添加弹窗等待
        self.Popupwaiting()

        print("测试用例2：账号：1@~ 	密码：123456")
        self.TestCase(user="1@~ ", password="123456")
        self.Popupwaiting()

        #输入错误的账号和错误的密码
        print("测试用例3：输入错误的账号和错误的密码  账号：6662@！	密码：123456789")
        self.TestCase(user="6662@！", password="123456789")
        self.Popupwaiting()

        #输入账号为空或者密码为空的情况
        print("测试用例4：输入账号为空或者密码为空的情况  账号：zhangsan	密码：空")
        self.TestCase(user="zhangsan", password="")
        self.Popupwaiting()

        print("测试用例4：账号：空	密码：123456")
        self.TestCase(user="", password="123456")
        self.Popupwaiting()

        print("测试用例4：账号：空	密码：空")
        self.TestCase(user="", password="")
        self.Popupwaiting()

        print("----测试结束----\n")